//
//  CZStatus.swift
//  WeiBo
//
//  Created by apple on 16/6/27.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit

class CZStatus: NSObject {
    
    /// 微博创建时间
    var created_at: String?
    /// 微博ID
    var id: Int64 = 0
    /// 微博信息内容
    var text: String?
    /// 微博来源
    var source: String?{
        didSet{
            sourceAttr = getSourceStr(source)
        }
    }
    // 用户信息
    var user: CZUser?
    // 转发微博
    var retweeted_status:CZStatus?
    // 配图数组
    var pic_urls:[CZPictureInfo]?

    //		转发数
    var repostsStr: String?
    var reposts_count:Int = 0{
        didSet{
            repostsStr = getCountString(reposts_count, title: "转发")
        }
    }
    //		评论数
    var commentsStr: String?
    var comments_count:Int = 0{
        didSet{
            commentsStr = getCountString(comments_count, title: "评论")
        }
    }
    //		表态数
    var attitudesStr: String?
    var attitudes_count:Int = 0{
        didSet{
            attitudesStr = getCountString(attitudes_count, title: "赞")
        }
    }
 
 
    //  微博来源
    var sourceAttr: NSMutableAttributedString?
    // KVC构造函数
    init(dict:[String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            // 判断可选值是否存在 而且是否能转成字典
            guard let v = value as?[String: AnyObject] else {
                print("可选值不存在 或者是不能转成字典")
                return
            }
            // 手动字典转模型
            let u = CZUser(dict: v)
            // 对该属性赋值
            user = u
        }else if key == "retweeted_status" {
            // 判断可选值是否有值 而且是否可以转成字典
            guard let v = value as?[String: AnyObject] else{
                return
            }
            let s = CZStatus(dict: v)
            // 给转发模型赋值
            retweeted_status = s
            
        }else if key == "pic_urls" {
            // 判断可选值是否有值 而且是否可以转成数组
            guard let v = value as?[[String: AnyObject]] else{
                return
            }
            // 创建一个临时数组
            var tempArray:[CZPictureInfo] = [CZPictureInfo]()
            // 遍历数组
            for dic in v {
                let p = CZPictureInfo(dict: dic)
                tempArray.append(p)
            }
            // 对pic_urls 赋值
            pic_urls = tempArray
            
            
        }else {
           
            super.setValue(value, forKey: key)
        }
    }
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
    }
    
    func getCountString(count: Int, title: String) -> String{
        if count <= 0 {
            return title
        }else if count > 0 && count < 10000{
            return "\(count)"
        }else {
            let countF = CGFloat(count / 1000)/10
            var countStr = "\(countF)"
   
            if countStr.containsString(".0"){
                countStr = countStr.stringByReplacingOccurrencesOfString(".0", withString: "")
            }
            
            return "\(countStr)w"
        }
        
    }
    
    // 处理微博来源字符串
    func getSourceStr(source: String?) -> NSMutableAttributedString{
        // 判断可选值是否有值 而且是否包含"> 字符串
        guard let s = source where s.containsString("\">")  else {
            let r = "来自那遥远的地方"
            return getSourceAttr(r)
        }
        // 字符串的截取
        let start = s.rangeOfString("\">")!
        let end = s.rangeOfString("</a")!
        // 字符串截取
        let result = s.substringWithRange(start.endIndex..<end.startIndex)
        // 完成字符串拼接
        let r = "来自 \(result)"
        return getSourceAttr(r)
    }
    // 通过字符串生成富文本
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        // 实例化一个富文本
        let attr = NSMutableAttributedString(string: result)
        // 获取range
        let range = (result as NSString).rangeOfString("来自")
        // 设置属性
        attr.addAttributes([NSForegroundColorAttributeName: ThemeColor], range: range)
        return attr
    }


}
